import pygame
import time

from settings import Settings
from pygame.sprite import Group
from ship import Ship
from alien import Alien
from button import Button
import game_functions as gf


def run_game():
    # 初始化游戏和屏幕.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    button = Button(screen, "开始游戏")

    # 创造一只船
    ship = Ship(ai_settings, screen)

    # 外星人创造
    aliens = Group()

    #飞船子弹组
    bullets = Group()

    # 游戏开始时间
    next_time = time.time()

    # 游戏主循环
    while True:
        if ai_settings.is_begin:
            if round(time.time() - next_time) >= ai_settings.alien_create_time:
                next_time = time.time()
                aliens.add(Alien(ai_settings, screen))
            gf.update_model(screen, ship, aliens, bullets)
        gf.check_events(ai_settings, screen, ship, aliens, bullets, button)
        gf.update_screen(ai_settings, screen, ship, aliens, bullets, button)


run_game()
